# GCC路径
GCC_PATH = /usr/bin
PWD=`pwd`

# 目标名字
TARGET = STM32F103C8T6

BUILD_DIR = build

FreeRTOS ?= ../../../../FreeRTOS

C_SOURCES =  \
	../../STM32F10x_HAL/src/stm32f10x_gpio.c \
	../../STM32F10x_HAL/src/stm32f10x_rcc.c \
	Startup/CMSIS/CM3/CoreSupport/core_cm3.c \
	Startup/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c \
	Startup/CMSIS/CM3/DeviceSupport/ST/STM32F10x/misc.c \
	$(FreeRTOS)/tasks.c \
	$(FreeRTOS)/list.c \
	$(FreeRTOS)/queue.c \
	$(FreeRTOS)/timers.c \
	$(FreeRTOS)/portable/GCC/ARM_CM3/port.c \
	$(FreeRTOS)/portable/MemMang/heap_4.c \
	main.c

######################################

# ASM sources
ASM_SOURCES =  \
	Startup/startup_stm32f10x_md.s

######################################
# building variables
######################################

# debug build?
DEBUG = 1
# optimization
OPT = -Og

#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m3

# fpu
# NONE for Cortex-M0/M0+/M3
# float-abi
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS =

# C defines
C_DEFS =  \
-DUSE_STDPERIPH_DRIVER \
-DSTM32F10X_MD

# AS includes
AS_INCLUDES = \
	-IFreeRTOS\Source

# C includes
C_INCLUDES =  \
	-IStartup/CMSIS/CM3/CoreSupport \
	-IStartup \
	-IStartup/CMSIS/CM3/DeviceSupport/ST/STM32F10x \
	-I../../STM32F10x_HAL/inc \
	-I../../STM32F10x_HAL/src \
	-I$(FreeRTOS)/include \
	-I$(FreeRTOS)/portable/GCC/ARM_CM3 \
	-I$(FreeRTOS) \
	-I$(PWD)/../../Drivers \
	-I$(PWD) \
	-I../../Common/include

# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32F103C8Tx_FLASH.ld

# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@

$(BUILD_DIR):
	mkdir $@

#######################################
# clean up
#######################################
clean:
	-rm -fR $(BUILD_DIR)

#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# *** EOF ***

